package GUI;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.Border;

import composite.DocumentManager;
import composite.DocumentManager.FuncButtonsListener;

/**
 * Panel with function buttons
 */
@SuppressWarnings("serial")
public class FuncPanel extends JPanel{
	/** Current chosen color */
	public static Color current_color = Color.black;

	// Global variables to connect the main class with the subclasses
	public static JButton button_color;
	public static JColorChooser chooser;
	
	/**
	 * Create function the panel
	 * @param docuMan - object of the DocumenManager, to delegate listeners to (bad design?) 
	 */
	public FuncPanel(DocumentManager docuMan){
		// Layout for this panel (row, columns)
		setLayout(new GridLayout(1, 0));
		Border border = BorderFactory.createTitledBorder("Functions");
		setBorder(border);
		
		// Undo and rendo buttons, listeners are delegated to the DocumentManager class
		FuncButtonsListener buttons_listener = docuMan.new FuncButtonsListener();
		JButton button_undo = new JButton("Undo");
		add(button_undo);
		button_undo.addActionListener(buttons_listener);
		JButton button_redo = new JButton("Redo");
		add(button_redo);
		button_redo.addActionListener(buttons_listener);
		
		// Color choser
		// Create the color chooser dialog
		Color initialColor = Color.black;
		chooser = new JColorChooser(initialColor);
		chooser.getSelectionModel().addChangeListener(docuMan.new ColorChange());

		// Create the color button
		button_color = new JButton("Color");
		add(button_color);
		button_color.addActionListener(new ShowColorChooserAction(this));
	}
	
	/**
	 * This color button action creates and shows a color choser dialog.
	 */
	public class ShowColorChooserAction implements ActionListener {
	    JDialog dialog;

	    // Create the dialog that contains the color chooser
	    ShowColorChooserAction(JPanel panel) {
	        dialog = JColorChooser.createDialog(panel, "Choose color.", false, chooser, null, null);
	    }
	    
	    // Called when the color button is pressed
	    public void actionPerformed(ActionEvent evt) {
	        // Show dialog
	        dialog.setVisible(true);
	        //setEnabled(false); (?)
	    }
	}
	
}// FuncPanel
